package com.fuqi.android.zhongcai.base.view.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.fuqi.android.zhongcai.R;
import com.fuqi.android.zhongcai.base.mvp.presenter.RegisterPresenterImpl;
import com.fuqi.android.zhongcai.base.mvp.presenter.RegisterPresenterInter;
import com.fuqi.android.zhongcai.base.mvp.view.IRegisterView;
import com.fuqi.android.zhongcai.base.request.api.MsgVerifyCodeApi;
import com.fuqi.android.zhongcai.base.request.result.MsgVerifyCodeResult;
import com.fuqi.android.zhongcai.base.request.result.RegisterResult;
import com.fuqi.android.zhongcai.base.view.widget.CountDownButton;
import com.fuqi.android.zhongcai.utils.ActivityUtils;
import com.fuqi.framework.network.code.HandleCode;
import com.fuqi.framework.network.http.RequestManager;

import butterknife.Bind;
import butterknife.OnClick;


public class RegisterActivity extends BaseActivity implements CountDownButton.CountDownDelegateListener,IRegisterView {

    @Bind(R.id.btn_getcode)
    CountDownButton btnGetcode;

    @Bind(R.id.et_register_phone)
    EditText et_register_phone;
    @Bind(R.id.et_register_newpass)
    EditText et_register_newpass;
    @Bind(R.id.et_register_vercode)
    EditText et_register_vercode;
    @Bind(R.id.et_register_replypass)
    EditText et_register_replypass;
    @Bind(R.id.nowregister)
    Button nowregister;
    @Bind(R.id.nowlogin)
    TextView nowlogin;
    RegisterPresenterInter registerPresenter;
    private String et_register_phonestr,et_register_newpassstr,et_register_vercodestr,et_register_replypassstr;

    @OnClick({R.id.btn_getcode,R.id.nowlogin,R.id.nowregister})
    public void onBtnClick(View view) {
        et_register_phonestr     =  et_register_phone.getText().toString().trim();
        switch (view.getId()){
            case R.id.btn_getcode:
                if(TextUtils.isEmpty(et_register_phonestr)){
                    ActivityUtils.toast("请输入手机号");
                    break;
                }else if (et_register_phonestr.length() < 11){
                    ActivityUtils.toast("手机号长度输入不合法");
                    break;

                } else{
                    //获取验证码
                    RequestManager.getInstance().call(new MsgVerifyCodeApi(new MsgVerifyCodeApi.MsgVerifyCodeParams(et_register_phonestr), resultListener, errorListener));
                }
                break;
            case R.id.nowregister:

                et_register_newpassstr   =  et_register_newpass.getText().toString().trim();
                et_register_vercodestr   =  et_register_vercode.getText().toString().trim();
                et_register_replypassstr =  et_register_replypass.getText().toString().trim();

                if(TextUtils.isEmpty(et_register_phonestr)){
                    //此处可以加一个手机号码的验证
                    ActivityUtils.toast("请输入手机号");
                    break;
                }else if(TextUtils.isEmpty(et_register_vercodestr)){
                    ActivityUtils.toast("请输入验证码");
                    break;
                }else if(TextUtils.isEmpty(et_register_newpassstr)){
                    ActivityUtils.toast("请输入密码");
                    break;
                }else if(TextUtils.isEmpty(et_register_replypassstr)){
                    ActivityUtils.toast("请再次输入密码");
                    break;
                }else if(!et_register_replypassstr.equals(et_register_newpassstr)){
                    ActivityUtils.toast("两次输入的密码不一致，请重新输入");
                    break;
                }else{
                    registerPresenter.registerDo(et_register_phonestr,et_register_newpassstr,et_register_vercodestr);

                }

                break;
            case R.id.nowlogin:
                finish();
                break;
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        registerPresenter = new RegisterPresenterImpl(this);
        titleBar.setTitle("免费注册");
    }


    Response.Listener<MsgVerifyCodeResult> resultListener = new Response.Listener<MsgVerifyCodeResult>() {

        @Override
        public void onResponse(MsgVerifyCodeResult response) {

            if (HandleCode.requestSuccess()) {
                btnGetcode.countDown();
                ActivityUtils.toast("验证码发送成功，请注意查收");
//            phoneId = response.getPhoneId();
//            phoneCode = response.getPhoneCode();
//            et_rgvalidate_code.setText(phoneCode);
            }else {

                ActivityUtils.toast("发送验证码失败");
            }
        }
    };


    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.i("juner", "juner  error.getMessage(): " + error.getMessage());
        }
    };


    @Override
    public void onCountDown(int seconds) {
        btnGetcode.setText("" + seconds);
    }

    @Override
    public void onCountDownonFinished() {
        btnGetcode.setText("重新获取");
    }


    @Override
    public void registerSuccess(RegisterResult registerResult) {


        ActivityUtils.toast("注册成功");
        finish();
    }

    @Override
    public void registerFailed() {

        ActivityUtils.toast("注册失败");

    }

    @Override
    public void initView() {

    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

    }

    @Override
    public boolean hasTitleBar() {
        return true;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_register;
    }
}
